class Node():
    def __init__(self,value):
        # self.pre = None
        self.next = None
        self.value = value
class SinCycLink():    
    def __init__(self,node=None):
        self.__head = node
    
    def is_Emply(self):

        return self.__head == None
    def length(self): 
        cur = self.__head
        counts  = 1
        if self.is_Emply():
            return 0
        while cur.next != self.__head:
            counts += 1
            cur = cur.next
        return counts
    def travel(self):
        cur = self.__head
        if self.is_Emply():
            print("")
        else:
            while cur.next != self.__head:
                print(cur.value,end="")
                cur = cur.next
            #退出循环，尾结点并未打印的
            print(cur.value)

    def  append(self,value):
        node = Node(value)
        if self.is_Emply():
            self.__head = node
            node.next = self.__head
            return
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            cur.next = node
            node.next = self.__head
            return
    def add(self,value):
        node = Node(value)
        if self.is_Emply():
            self.__head = node
            node.next = self.__head
        else:
            cur = self.__head
            while cur.next != self.__head:
                cur = cur.next
            node.next = self.__head
            self.__head = node
            cur.next = self.__head
    def insert(self,index,value):
        node = Node(value)
        if index <=1:
            node.next = self.__head
            self.__head =node
        else:    
            pos = 1
            cur = self.__head
            while pos < (index-1):
                cur = cur.next
            node.next = cur.next
            cur.next = node


    def search(self,value):
        if self.is_Emply():
            return False
        else:
            cur = self.__head
            while cur.next != self.__head:
                if cur.value == value:
                    return True
                cur = cur.next
            if cur.value == value:
                return True
            else:
                return False

    def delete(self,value):
        cur = self.__head
        pre = None
        if self.is_Emply():
            return 
        else:
            while cur.next != self.__head:
                if cur.value == value:
                    if cur.next == self.__head:
                        #如果是头结点的情况话。这是另一种情况啦
                        #找到尾结点
                        tail = self.__head
                        while tail.next != self.__head:
                            tail = tail.next
                        tail.next = cur.next
                        self.__head = cur.next
                    else:
                        pre.next = cur.next
                    return
                else:
                    pre = cur
                    cur = cur.next
            if cur.value == value:
                if cur == self.__head:
                    self.__head = None
                else:
                    pre.next = cur.next



if __name__=="__main__":
    cylink=SinCycLink()
    cylink.append(1)
    print(cylink.is_Emply())
    cylink.append(2)
    cylink.append(3)
    cylink.append(4)
    cylink.append(5)
    cylink.add(0)
    cylink.travel()
    cylink.insert(1,9)
    cylink.travel()
    # cylink.insert(4,10)
    # cylink.travel()
    # cylink.search(4)
    # cylink.travel()
    # cylink.delete(3)
    # cylink.travel()





    